package com.example.jinbo.helloword;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
    private DrawerLayout mDrawerLayout ;
    private ListView mList;
    private String[] mDetailTitles={"标题1","标题2","标题3","标题4","标题5","标题6","标题7","标题8"};
    private String[] mItems={"item1","item2","item3","item4","item5","item6",};
    private  boolean isMain=true;
   // private View mMainWord;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mList=(ListView)findViewById(R.id.left_drawer);
        //mMainWord=findViewById(R.id.main_word);
        //getActionBar().setDisplayHomeAsUpEnabled(true);
        Fragment mfFragment = new DetailFragment();
        Bundle args = new Bundle();
        args.putInt(DetailFragment.ITEM_ID, -1);
        //创建一个FragmentManager管理器，根据getFragmentManager的返回值可以了解到，此处是用来操作我们创建的DetailFragment对象的。
        mfFragment.setArguments(args);
        FragmentManager fragmentManager = getFragmentManager();
        //根据android api所说，用当前的DetailFragment来替换之前存在的Fragment。
        fragmentManager.beginTransaction()
                .replace(R.id.content_frame, mfFragment).commit();
        final Adapter myAdapter=new Adapter(this,mItems);
        mList.setAdapter(myAdapter);
        mList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                myAdapter.setSelectItem(position);
                selectItem(position);
            }
        });

    }
    //在主界面中切换内容
    private void selectItem(int position) {
       // mMainWord.setVisibility(View.INVISIBLE);
        //创建一个Fragment用来显示一个你所需要用的视图
        Fragment mfFragment = new DetailFragment();
        Bundle args = new Bundle();
        args.putInt(DetailFragment.ITEM_ID, position);
        //创建一个FragmentManager管理器，根据getFragmentManager的返回值可以了解到，此处是用来操作我们创建的DetailFragment对象的。
        mfFragment.setArguments(args);
        FragmentManager fragmentManager = getFragmentManager();
        //根据android api所说，用当前的DetailFragment来替换之前存在的Fragment。
        fragmentManager.beginTransaction()
                .replace(R.id.content_frame, mfFragment).commit();
        //以下不解释
        isMain=false;
        getActionBar().setDisplayHomeAsUpEnabled(true);
        getActionBar().setTitle(mDetailTitles[position]);
        mDrawerLayout.closeDrawer(mList);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }
    @Override
    public void onBackPressed() {
        if(isMain)
            finish();
        else
        {
            Fragment mfFragment = new DetailFragment();
            Bundle args = new Bundle();
            args.putInt(DetailFragment.ITEM_ID,-1);
            //创建一个FragmentManager管理器，根据getFragmentManager的返回值可以了解到，此处是用来操作我们创建的DetailFragment对象的。
            mfFragment.setArguments(args);
            FragmentManager fragmentManager = getFragmentManager();
            //根据android api所说，用当前的DetailFragment来替换之前存在的Fragment。
            fragmentManager.beginTransaction()
                    .replace(R.id.content_frame, mfFragment).commit();
            isMain=true;
        }
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

       // //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }
        if (id == android.R.id.home) {
            //return true;
           // Toast.makeText(this, "fasfas",Toast.LENGTH_LONG).show();
          //  finish();
          //  getFragmentManager().popBackStack();
            isMain=true;
            getActionBar().setDisplayHomeAsUpEnabled(false);
            getActionBar().setTitle("HelloWord");
            Fragment mfFragment = new DetailFragment();
            Bundle args = new Bundle();
            args.putInt(DetailFragment.ITEM_ID,-1);
            //创建一个FragmentManager管理器，根据getFragmentManager的返回值可以了解到，此处是用来操作我们创建的DetailFragment对象的。
            mfFragment.setArguments(args);
            FragmentManager fragmentManager = getFragmentManager();
            //根据android api所说，用当前的DetailFragment来替换之前存在的Fragment。
            fragmentManager.beginTransaction()
                    .replace(R.id.content_frame, mfFragment).commit();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

}
